package com.meiqiongproperty.yiguanjia.fragment.home.shopping;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.google.gson.Gson;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.commom.ViewConstant;
import com.meiqiongproperty.yiguanjia.databinding.MoreProductListLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.shoppingadapter.MoreProductListAdapter;
import com.meiqiongproperty.yiguanjia.fragment.shopcart.CartFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendFreshFruitList;
import com.meiqiongproperty.yiguanjia.httpbean.shopping.ShopListBean;
import com.meiqiongproperty.yiguanjia.ui.popupWindow.ProductListSortPopupWindow;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/29.
 */

public class MoreProductListFragment extends BaseFragment {
    MoreProductListLayoutBinding binding;
    // List<Product> list=new ArrayList<>();
    MoreProductListAdapter adapter;
    String cate, typeName;
    List<ShopListBean.ShoplistBean> shop_list = new ArrayList<>();
    ProductListSortPopupWindow sortPopupWindow;
    int type = 0;
    String sortType = "";
    String sort = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.more_product_list_layout, container, false);
        binding.setClick(this);

        savedInstanceState = getArguments();
        if (savedInstanceState != null) {
            cate = savedInstanceState.getString("SecondCate");
            typeName = savedInstanceState.getString("name");
        }
        init();
        getShopList(null, null, null, null, null);
        return binding.getRoot();
    }

    public void init() {
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.moreListProduct.setLayoutManager(manager);
        binding.tvProductType.setText(typeName);

        binding.moreListProduct.setHasFixedSize(true);
        //设置refresh
        binding.refreshShoplist.setPullRefreshEnable(true);
        binding.refreshShoplist.setPullLoadEnable(false);
        binding.refreshShoplist.setAutoRefresh(false);

        binding.refreshShoplist.setPinnedTime(1000);
        binding.refreshShoplist.setMoveForHorizontal(true);

        binding.refreshShoplist.setXRefreshViewListener(new com.meiqiongproperty.yiguanjia.ui.PullToRefresh.XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh() {
                super.onRefresh();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        binding.refreshShoplist.stopRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                super.onLoadMore(isSlience);
            }

            @Override
            public void onRelease(float direction) {
                super.onRelease(direction);
            }

            @Override
            public void onHeaderMove(double offset, int offsetY) {
                super.onHeaderMove(offset, offsetY);
            }
        });
    }


    public void BackToFront(View view) {
        sendKeyBackEvent();
    }

    public void SearchClick(View view) {
        getShopList(binding.productSearch.getText().toString(), sortType + "", sort, "1", "50");
    }

    public void shoppingCartClick(View view) {
        ViewConstant.hideLeft = 1;
        GenericFragmentActivity.start(getActivity(), CartFragment.class, new Bundle());
    }

    /**
     * 下拉商品类别
     *
     * @param view
     */
    public void productChooseListClick(View view) {

    }

    /**
     * 排序类别
     */
    public void productSortClick(View view) {
        setSortPopupWindow();
    }

    /**
     * 请求商品列表
     */
    public void getShopList(String KeyWord, String orderType, String OrderBy, String pageNo, String PageSize) {
        SendFreshFruitList bean = new SendFreshFruitList();
        bean.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        bean.setFirstCate("1");
        bean.setSecondCate(cate);
        if (KeyWord != null) {
            bean.setKeyWord(KeyWord);
        }
        if (orderType != null) {
            bean.setOrderType(orderType);
        }
        if (OrderBy != null) {
            bean.setOrderBy(OrderBy);
        }
        if (pageNo != null) {
            bean.setPageNo(pageNo);
        }
        if (PageSize != null) {
            bean.setPageSize(PageSize);
        }

        ApiService.getShopList(getActivity(), new SimpleHttpCallback<ShopListBean>() {
            @Override
            public void onNext(ShopListBean shopListBean) {
                shop_list.clear();
                Gson gson = new Gson();
                LogUtil.d("shopListBean==================>" + gson.toJson(shopListBean));
                shop_list.addAll(shopListBean.getShoplist());
                adapter = new MoreProductListAdapter(getActivity(), shop_list);

                binding.moreListProduct.setAdapter(adapter);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Bundle bundle = new Bundle();
                        bundle.putString("productId", shop_list.get(position).getProductId() + "");
                        GenericFragmentActivity.start(getActivity(), ShoppingDetailFragment.class, bundle);
                    }
                });
            }
        }, Utils.getRequestBody(bean));
    }

    /**
     * 排序类别下拉
     */
    public void setSortPopupWindow() {
        binding.cover.setVisibility(View.VISIBLE);
        sortPopupWindow = new ProductListSortPopupWindow(getActivity());
        sortPopupWindow.setBackgroundDrawable(null);
        sortPopupWindow.showAtLocation(binding.rlProductSort, Gravity.CENTER_HORIZONTAL
                , 0, binding.rlProductSort.getBottom() - 36);
        sortPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                binding.cover.setVisibility(View.GONE);
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_default).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "";
                sortType = "0";
                binding.tvSort.setText("默认排序");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.drop_down_list, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_time_desc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "DESC";
                sortType = "1";
                binding.tvSort.setText("添加时间");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.drop_down_list, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_time_asc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "ASC";
                sortType = "1";
                binding.tvSort.setText("添加时间");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.up_arrow, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_price_desc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "DESC";
                sortType = "2";
                binding.tvSort.setText("价格");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.drop_down_list, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_price_asc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "ASC";
                sortType = "2";
                binding.tvSort.setText("价格");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.up_arrow, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_sale_num_desc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "DESC";
                sortType = "3";
                binding.tvSort.setText("销量");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.drop_down_list, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

        sortPopupWindow.getContentView().findViewById(R.id.sort_sale_num_asc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sort = "ASC";
                sortType = "3";
                binding.tvSort.setText("销量");
                Utils.setCompoundDrawablesWithTextView(getActivity(), binding.tvSort, R.mipmap.up_arrow, "right");
                getShopList(binding.productSearch.getText().toString(), sortType, sort, "1", "50");
                sortPopupWindow.dismiss();
            }
        });

    }

}
